﻿using SuperX.Common.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace SuperX.Helper
{
    public class WebapiHelper
    {
        public Uri Uri { get; set; }

        public HttpWebRequest RequestClient { get; set; }

        public WebapiHelper(Uri uri)
        {
            Uri = uri;
        }

        public WebapiHelper(string uriString) : this(new Uri(uriString))
        {
        }

        public Response<T> Request<T>(string method, object para, Dictionary<string, string> headers = null, int timeout = 100000)
        {
            try
            {
                HttpWebRequest requestClient = (HttpWebRequest)WebRequest.Create(Uri);
                requestClient.ContentType = "application/json";
                requestClient.Timeout = timeout;
                string requestID = Guid.NewGuid().ToString();
                requestClient.Method = method;
                if (headers != null)
                {
                    foreach (var item in headers)
                    {
                        requestClient.Headers.Add(item.Key, item.Value);
                    }
                }
                if (para != null)
                {
                    var requesetString = JsonHelper.JsonSerialize(para);
                    var data = Encoding.UTF8.GetBytes(requesetString);
                    requestClient.ContentLength = data.Length;
                    using (var stream = requestClient.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                else
                {
                    requestClient.ContentLength = 0;
                }
                var response = (HttpWebResponse)requestClient.GetResponse();
                using (var responseStream = response.GetResponseStream())
                using (var reader = new StreamReader(responseStream))
                {
                    var responseStr = reader.ReadToEnd();
                    if (responseStr.Length > 0)
                    {
                        return new Response<T>()
                        {
                            IsSuccess = true,
                            Data = JsonHelper.JsonDeserialize<T>(responseStr)
                        };
                    }
                    else
                    {
                        return new Response<T>()
                        {
                            IsSuccess = false,
                            Data = default
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                return new Response<T>()
                {
                    IsSuccess = false,
                    Error = new Error($"MethodName:{method}\r\n{ex.ToString()}", 0),
                    Data = default
                };
            }
        }

        public Response<T> POST<T>(object para)
        {
            return Request<T>("POST", para);
        }

        public Response<T> POST<T>(object para, int timeout)
        {
            return Request<T>("POST", para, timeout: timeout);
        }

        public Response<T> GET<T>(object para)
        {
            return Request<T>("GET", para);
        }

        public Response<T> PUT<T>(object para)
        {
            return Request<T>("PUT", para);
        }
    }
}